<?php
/**
 * Core_Model_Session
 * session model
 * 
 * @author Adrian Stolarski
 */
class Core_Model_Session implements Core_Model_Mainable, Core_Model_Sessioncheckoutable, 
        Core_Model_Datacheckoutable, Core_Model_Xssprotectable, Core_Model_Decidable {
    
    /**
     *
     * @var type $get_data
     * getdatabasedata handler
     */
    public $get_database_data;
    
    /**
     *
     * @var type $custom_session
     * custom session handler
     */
    public $custom_session;
    
    /**
     *
     * @var type $current_session
     * current session handler
     */
    public $current_session;
    
    /**
     *
     * @var type $new_session
     * new session handler
     */
    public $new_session;
    
    /**
     *
     * @var type $xss_handler
     * xss protection handler
     */
    public $xss_handler;
    
    /**
     * main()
     * session initialization
     * 
     * @param array $variables
     * @throws Exception
     */
    public function main(array $variables = null) {
        try {
            if($variables != null){
                //$user_id = $this->get_data($variables);
                //$this->decision($user_id, $action = null);
            }
            else if($variables == null){
                $this->session_checkout();
            }
            else {
                $message = new Core_Data_Badsessionmessage;
                throw new Exception($message->message("0"));
            }
        }
        catch(Exception $exception){
            echo $exception->getMessage();
        }
    }
    
    /**
     * session_checkout()
     * session checkout
     */
    public function session_checkout() {
        if($_SESSION['user_name'] == null){
            $this->custom_session = new Core_Data_Customsession;
            $this->custom_session->session_init($value = null);
        }
        else {
            $this->current_session = new Core_Data_Currentsession;
            $this->current_session->session_init();
        }
    }
    
    /**
     * data_checkout()
     * get session data
     * 
     * @param array $variables
     */
    public function data_checkout(array $variables = null) {
        $variables = $this->xss_entrypoint($variables, "all_xss_filters");
        $this->get_database_data = new Core_Data_Getdatabasedata;
        $user_id = $this->get_database_data->session_data_checkout($variables);
        return $user_id;
    }

    /**
     * xss_entrypoint()
     * xss protection
     * 
     * @param array $variables
     * @param type $action
     * @return type $variables
     */
    public function xss_entrypoint(array $variables = null, $action = null) {
        $this->xss_handler = new Core_Controller_Xssprotection;
        $variables = $this->xss_entrypoint($variables, $action);
        return $variables;
    }
    
    /**
     * decision()
     * set new session decision
     * 
     * @param type $value
     * @param type $action
     */
    public function decision($value = null, $action = null) {
        
        if((isset($value)) && (!is_null($value)) && ($value != '')){
            $this->new_session = new Core_Data_Newsession;
            $this->new_session->session_init($value);
        }
        else {
            $this->main();
        }
    }

}
